<template>
  <el-main>
    <el-button class="list-btn" type="primary" icon="el-icon-plus" @click="createItem" v-if="btnShow">创建</el-button>
    <el-table :data="tableData" :row-class-name="tableRowClassName" v-if="listShow">
      <el-table-column prop="num" label="序号"></el-table-column>
      <el-table-column prop="name" label="歌单名"></el-table-column>
      <el-table-column prop="introduction" label="介绍"></el-table-column>
      <el-table-column prop="updateTime" label="更新"></el-table-column>
      <el-table-column prop="id" label="操作">
        <template slot-scope="scope">
          <el-button
            class="list-btn"
            type="success"
            @click="editItem(scope.$index,scope.row)"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button 
            class="list-btn"
            type="danger"
            @click="deleteItem(scope.$index,scope.row)"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-form ref="form" :model="form" label-width="80px" v-if="editShow">
      <el-form-item label="歌单名">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="介绍">
        <el-input v-model="form.introduction"></el-input>
      </el-form-item>
      <el-form-item label="更新日期">
        <el-col :span="11">
          <el-date-picker type="date" placeholder="选择日期" v-model="form.updateTime"></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item label="用户">
        <el-select v-model="form.user" value-key="id" placeholder="请选择用户">
        <el-option label="无" :value="null" ></el-option>
          <el-option v-for="user in edit.users" :key="user.id" :label="user.username" :value="user"></el-option>
        </el-select>
      </el-form-item>
     
      <el-form-item label="用户收藏">
        <el-checkbox-group v-model="form.users" v-if="newShow==false">
          <el-checkbox
            v-for="user in edit.users"
            :key="user.id"
            :label="user"
            :checked="check(edit.songlist.users, user)"
          >{{user.username}}</el-checkbox>
        </el-checkbox-group>
        <el-checkbox-group v-model="form.users" v-else>
          <el-checkbox
                  v-for="user in edit.users"
                  :key="user.id"
                  :label="user"
          >{{user.username}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      
      <el-form-item label="歌曲">
        <el-checkbox-group v-model="form.songs" v-if="newShow==false">
          <el-checkbox
            v-for="song in edit.songs"
            :key="song.id"
            :label="song"
            :checked="check(edit.songlist.songs, song)"
            name="type"
          >{{song.name}}</el-checkbox>
        </el-checkbox-group>
        <el-checkbox-group v-model="form.songs" v-else>
          <el-checkbox
              v-for="song in edit.songs"
              :key="song.id"
              :label="song"
            name="type"
          >{{song.name}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" @click="onSubmit" v-if="newShow==false">确定</el-button>
        <el-button type="primary" @click="add" v-else>确定</el-button>
        <el-button @click="back">取消</el-button>
      </el-form-item>
    </el-form>
  


  </el-main>
</template>

<script type="module">
var songlists = [];
var formData = [];
var editData = [];
var delFlag = false;
module.exports = {
  data: function() {
    return {
      tableData: songlists,
      editShow: false,
      listShow: true,
      newShow: false,
      btnShow: true,
      edit: editData,
      emptyForm: {
        id: "",
        name: "",
        introduction: "",
        updateTime: "",
        user: "",
        users: [],
        songs: [],
      },
      form: {
        id: "",
        name: "",
        introduction: "",
        updateTime: "",
        user: "",
        users: [],
        songs: [],
      }
    };
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex;
    },
    gettable() {
      $.ajax({
        url: "/listAllSonglists",
        type: "get",
        dataType: "json",
        async: false,
        success(data) {
          for (let i in data.songlists) {
              songlists[i] = {
                id: data.songlists[i].id,
                //非对象及其非数组信息
                num: i,
                name: data.songlists[i].name,
                introduction: data.songlists[i].introduction,
                updateTime: data.songlists[i].updateTime,
              };
          }
        }
      });
    },
    deleteItem(item, index) {
      let id = '{"id": ' + index.id + "}";
      $.ajax({
        url: "/deleteSonglist",
        type: "post",
        data: JSON.parse(id),
        async: false,
        success(data) {
          if(data) {
            delFlag = true;
          } else {
            delFlag = false;
          } 
        }
      });
      if(delFlag) {
        this.$message({
          message: '恭喜你，删除成功',
          type: 'success'
        });
        setTimeout(function(){
          window.location.href = "/index?page=11";
        }, 1500);
      } else {
        this.$message.error('错误窝，删除失败');
      }
    },
    editItem(item, index) {
      this.newShow = false;
      this.listShow = false;
      this.editShow = true;
      this.btnShow = false;
      var id = '{"id": ' + index.id + "}";
      $.ajax({
        url: "/editSonglist",
        type: "get",
        dataType: "json",
        async: false,
        data: JSON.parse(id),
        success(data) {
          editData = {
          	  //集合数据和已存在容器
              songlist: data.songlist,
              users: data.users,
              songs: data.songs,
          };
          formData = {
              id: data.songlist.id,
              name: data.songlist.name,
              introduction:data.songlist.introduction,
              updateTime: data.songlist.updateTime,
              user: data.songlist.user,
              users: [],
              songs: [],
          };
        }
      });
      this.edit = editData;
      this.form = formData;
    },
    check(objs, obj) {
      for (let i in objs) {
        if (objs[i].id == obj.id) return true;
      }
      return false;
    },
    onSubmit() {
      $.ajax({
        url: "/updateSonglist",
        type: "post",
        contentType: "application/json",
        async: false,
        data: JSON.stringify(this.form),
        success(data) {
          window.location.href = "/index?page=11";
        }
      });
    },
    back() {
      this.listShow = true;
      this.editShow = false;
      this.btnShow = true;
      this.emptyForm = {
        id: "",
        name: "",
        introduction: "",
        updateTime: "",
        user: "",
        users: [],
        songs: [],
      };
    },
    createItem() {
      this.newShow = true;
      this.listShow = false;
      this.editShow = true;
      this.btnShow = false;
      $.ajax({
        url: "/newSonglist",
        type: "get",
        dataType: "json",
        async: false,
        success(data) {
          editData = {
              users: data.users,
              songs: data.songs,
          };
        }
      });
      this.edit = editData;
      this.form = this.emptyForm;
    },
    add() {
      $.ajax({
        url: "/addSonglist",
        type: "post",
        contentType: "application/json",
        async: false,
        data: JSON.stringify(this.form),
        success(data) {
          window.location.href = "/index?page=11";
        }
      });
    }
  },
  mounted: function() {
    this.gettable();
  }
};
</script>